<?php

namespace App\Http\Controllers\OA;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Logic\AdminLogic;
use Illuminate\Http\Request;

/**
 * 行政相关
 */
class AdminController extends Controller
{
    protected $leaveRole = [
        215,//项目经理
        219,//行政总监
        218,//财务总监
        214,//事业部总监
        217,//其他
        213,//销售经理
        222,//财务副总
        224,//客户经理
        225,//出纳
        226,//审计
        227,//采购经理
    ];//请假角色
    protected $baoxiaoRole = [
        214,//事业部总监
        219,//行政总监
        213,//销售经理
        222,//财务副总
        216,//策略副总
        220,//设计总监
        212,//采购总监
        211,//副总
        210,//总经理
        218,//财务总监
        225,//出纳
    ];//行政报销角色
    //签到
    public function addSignIn(Request $request)
    {
        try{
            $rules = ['desc'=>'required|string'];
            $this->verify($request->all(),$rules);
            $params = $this->mergeRequest($request);
            $result = (new AdminLogic())->addSignIn($params);
            ret_json('ok','签到成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //行政报销
    public function addBaoXiao(Request $request)
    {
        try{
            $rules = [
                'desc'=>'required|string',
                'title'=>'required|string',
                'money'=>'numeric|required',
                'is_save'=>'required|numeric|in:0,1',
                'start'=>'date|required'
            ];
            $this->verify($request->all(),$rules);
            $params = $this->mergeRequest($request);
            $result = (new AdminLogic())->addBaoXiao($params);
            ret_json('ok','报销添加成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取报销列表
    public function baoXiaoList(Request $request)
    {
        try{
            $rules = [
                'title'=>'sometimes|string|nullable',
                'state'=>'present|nullable|numeric|in:0,1,2',
                'type'=>'sometimes|nullable|numeric',
                'page'=>'required|numeric|min:1',
                'page_size'=>'required|numeric|min:10',
            ];
            $this->verify($request->all(),$rules);
            $params = $this->mergeRequest($request);
            $list = (new AdminLogic())->baoXiaoList($params);
            ret_json('ok','报销添加成功',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取可申请的行政报销
    public function getApplyList(Request $request){
        $params = $this->mergeRequest($request);
        $list = (new AdminLogic())->getApplyList($params);
        ret_json('ok','请求成功',$list);
    }

    //是否可以申请加班
    public function isAbleApply(Request $request){
        $params = $this->mergeRequest($request);
        $roleid = $params['roleid'];
        $isAble = $roleid == 215 ;//加班,物料,交通
        //行政,休假
        $is_baoxiao = in_array($roleid,$this->baoxiaoRole);
        $is_xiujia = in_array($roleid,$this->leaveRole);

        ret_json('ok','ok',['is_able'=>$isAble,'is_baoxiao'=>$is_baoxiao,'is_xiujia'=>$is_xiujia]);
    }

    //添加行政报销
    public function addAdminWork(Request $request)
    {
        try{
            $rules = [
                'type'=>'required|numeric',
                'title'=>'required|string',
                'is_save'=>'required|in:0,1'
            ];
            $this->verify($request->all(),$rules);
            $this->checkHeader($request->header());
            $params = $this->mergeRequest($request);
            $result = (new AdminLogic())->addAdminWork($params);
            ret_json('ok','添加成功');

        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //行政数据详情
    public function baoXiaoDetail(Request $request)
    {
        try{
            $rules = [
                'id'=>'required|exists:admin,id',
            ];
            $this->verify($request->all(),$rules);
            $params = $this->mergeRequest($request);
            $info = (new AdminLogic())->getAdminDetail($params);

            ret_json('ok','ok',$info);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //编辑行政数据
    public function baoXiaoEdit(Request $request)
    {
        try{
            $rules = [
                'id'=>'required|exists:admin,id',
                'is_save'=>'required|numeric|in:0,1',
                'newData'=>'required|array',//新数据
            ];
            $this->verify($request->all(),$rules);
            $this->checkHeader($request->header());
            $params = $this->mergeRequest($request);
            $result = (new AdminLogic())->editBaoXiao($params);
            $msg = $params['is_save'] ? '保存' : '修改';
            ret_json('ok',$msg.'成功');
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

    //获取考勤数据
    public function getLoginList(Request $request)
    {
        try{
           $rules = [
               'page'=>'required|min:1|numeric',
               'page_size'=>'required|min:10|numeric',
               'uid'=>'sometimes|nullable',
               'date'=>'sometimes|nullable|date'
           ];
           $params = $request->all();
           $this->verify($params,$rules);

           $list = (new AdminLogic())->getLoginList($params);
           ret_json('ok','ok',$list);
        }catch (\Exception $e){
            ret_json('err',$e->getMessage());
        }
    }

}
